package com.dtw.ditouwang.ui.more;

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.dtw.ditouwang.R;
import com.dtw.ditouwang.beans.CommentDetialBean;
import com.dtw.ditouwang.models.WebApi;
import com.dtw.ditouwang.ui.OnItemClickListener;

import java.util.List;

public class CommentMoreAdapter extends RecyclerView.Adapter<CommentMoreAdapter.Holder> {
    private List<CommentDetialBean.DataBean.SecondBean> commentMoreList;
    private OnItemClickListener onItemClickListener;

    public CommentMoreAdapter(List<CommentDetialBean.DataBean.SecondBean> commentMoreList){
        this.commentMoreList = commentMoreList;
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener){

        this.onItemClickListener = onItemClickListener;
    }

    @NonNull
    @Override
    public Holder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        return new Holder(LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_comment, viewGroup, false));
    }

    @Override
    public void onBindViewHolder(@NonNull Holder holder, int i) {
        holder.name.setText(commentMoreList.get(i).getNickname());
        holder.content.setText(commentMoreList.get(i).getC_info());
        holder.awesome.setText(commentMoreList.get(i).getZan() + "");
        holder.awesome.setSelected(commentMoreList.get(i).getIs_click()==1);
        holder.time.setText(commentMoreList.get(i).getTimes());
        if(!TextUtils.isEmpty(commentMoreList.get(i).getHead())) {
            Glide.with(holder.head).load(WebApi.BaseUrl + commentMoreList.get(i).getHead().replace("\\", "/")).into(holder.head);
        }
    }

    @Override
    public int getItemCount() {
        return commentMoreList.size();
    }

    class Holder extends RecyclerView.ViewHolder{
        TextView name, time, awesome, content;
        ImageView head;

        public Holder(@NonNull View itemView) {
            super(itemView);
            name = itemView.findViewById(R.id.text_name);
            time = itemView.findViewById(R.id.text_time);
            awesome = itemView.findViewById(R.id.text_awesome);
            content = itemView.findViewById(R.id.text_content);
            head = itemView.findViewById(R.id.img_head);
            awesome.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(onItemClickListener!=null){
                        if(awesome.isSelected()){
                            awesome.setSelected(false);
                            awesome.setText(Integer.parseInt(awesome.getText().toString())-1+"");
                        }else{
                            awesome.setSelected(true);
                            awesome.setText(Integer.parseInt(awesome.getText().toString())+1+"");
                        }
                        if(onItemClickListener!=null){
                            onItemClickListener.onItemClick(R.id.text_awesome, getAdapterPosition(),awesome.isSelected()?1:0);
                        }
                    }
                }
            });
        }
    }
}
